package com.longdaji.decoration.ui.pay;

import android.util.Log;

import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.bean.AddressBean;
import com.longdaji.decoration.data.bean.AvailableMoneyBean;
import com.longdaji.decoration.data.bean.AvailableScoreBean;
import com.longdaji.decoration.data.bean.CartBean;
import com.longdaji.decoration.data.bean.GoodsAccountBean;
import com.longdaji.decoration.data.bean.InviteCodeBean;
import com.longdaji.decoration.data.bean.LogisticsBean;
import com.longdaji.decoration.data.bean.OrderBean;
import com.longdaji.decoration.data.bean.PakageBean;
import com.longdaji.decoration.data.bean.PayBean;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.utils.RxUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

/**
 * Created by longtao on 2017/12/19.
 */

public class PayPresenter extends RxPresenter<PayContract.View> implements PayContract.Presenter {
    private final static String TAG = PayPresenter.class.getSimpleName();
    private DataManager mDataManager;

    @Inject
    public PayPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }


    @Override
    public void getAccountData(String userId, List<CartBean.Cart> cartGoodsList) {
        List<String> idList = new ArrayList<>();
        for (int i = 0; i < cartGoodsList.size(); i++) {
            idList.add(cartGoodsList.get(i).getCartGoodsRecordId());
        }
        Log.d(TAG, "deleteChartGoods idList=" + idList.toString());
        addSubscribe(mDataManager.accountFromChart(userId, idList)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<GoodsAccountBean>handleResult())
                .subscribeWith(new CommonSubscriber<GoodsAccountBean>() {
                    @Override
                    public void onSuccess(GoodsAccountBean goodsAccountBean) {
                        if (mView != null) {
                            Log.d(TAG, "goodsAccountBean =" + goodsAccountBean);
                            mView.showData(goodsAccountBean);
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast(msg);
                        }
                    }
                }));
    }

    @Override
    public void getDefaultAddress(String userId) {
        addSubscribe(mDataManager.getDefaultAddress(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<AddressBean.Address>handleResult())
                .subscribeWith(new CommonSubscriber<AddressBean.Address>() {
                    @Override
                    public void onSuccess(AddressBean.Address address) {
                        if (mView != null) {
                            Log.d(TAG, "address =" + address);
                            if (address != null) {
                                mView.showDefaultAddress(address);
                            } else {
                                mView.showNoDefaultAddress();
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.showNoDefaultAddress();
                        }
                    }
                }));
    }

    @Override
    public void getAddressList(String userId) {
        addSubscribe(mDataManager.getAddressList(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<AddressBean>handleResult())
                .subscribeWith(new CommonSubscriber<AddressBean>() {
                    @Override
                    public void onSuccess(AddressBean address) {
                        if (mView != null) {
                            Log.d(TAG, "AddressBean =" + address);

                            mView.showAddressList(address.getAddress());


                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast(msg);
                        }
                    }
                }));
    }

    @Override
    public void getPayOrderInfo(String orderId) {
        Map<String, Object> options = new HashMap<>();
        options.put("body", "桃心商品支付");
        options.put("subject", "桃心");
        options.put("out_trade_no", orderId);
        options.put("timeout_express", "30m");
        options.put("total_amount", "0.01");
        addSubscribe(mDataManager.getPayOrderInfo(options)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<PayBean>handleResult())
                .subscribeWith(new CommonSubscriber<PayBean>() {
                    @Override
                    public void onSuccess(PayBean payBean) {
                        Log.d(TAG, "orderInfo =" + payBean.getOrderInfo());
                        if (mView != null) {
                            mView.showGetOrderInfoSuccess(payBean.getOrderInfo());
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.showGetOrderInfoFail();
                        }
                    }
                }));
    }

    @Override
    public void getPakagePattern() {
        addSubscribe(mDataManager.getPakagePattern()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<List<PakageBean>>handleResult())
                .subscribeWith(new CommonSubscriber<List<PakageBean>>() {
                    @Override
                    public void onSuccess(List<PakageBean> pakageList) {
                        Log.d(TAG, "pakageList =" + pakageList);
                        if (mView != null) {
                            if (pakageList == null || pakageList.size() <= 0) {
                                mView.showNoPakage();
                            } else {
                                mView.showDefaultPakage(pakageList);
                            }

                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.showNoPakage();
                        }
                    }
                }));
    }

    @Override
    public void getLogisticsPattern() {
        addSubscribe(mDataManager.getLogisticsPattern()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<List<LogisticsBean>>handleResult())
                .subscribeWith(new CommonSubscriber<List<LogisticsBean>>() {
                    @Override
                    public void onSuccess(List<LogisticsBean> logisticsList) {
                        Log.d(TAG, "logisticsList =" + logisticsList);
                        if (mView != null) {
                            if (logisticsList == null || logisticsList.size() <= 0) {
                                mView.showNoLogistics();
                            } else {
                                mView.showDefaultLogistics(logisticsList);
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.showNoLogistics();
                        }
                    }
                }));
    }

    @Override
    public void getAvailableMoney(String userId) {
        addSubscribe(mDataManager.getAvailableMoney(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<AvailableMoneyBean>handleResult())
                .subscribeWith(new CommonSubscriber<AvailableMoneyBean>() {
                    @Override
                    public void onSuccess(AvailableMoneyBean availableMoneyBean) {
                        Log.d(TAG, "availableMoneyBean =" + availableMoneyBean);
                        if (mView != null) {
                            if (availableMoneyBean == null) {
                                mView.showDefaultAvailableMoney();
                            } else {
                                mView.showAvailableMoney(availableMoneyBean);
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.showDefaultAvailableMoney();
                        }
                    }
                }));
    }

    @Override
    public void getAvailableScore(String userId) {
        addSubscribe(mDataManager.getAvailableScore(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<AvailableScoreBean>handleResult())
                .subscribeWith(new CommonSubscriber<AvailableScoreBean>() {
                    @Override
                    public void onSuccess(AvailableScoreBean availableScoreBean) {
                        Log.d(TAG, "AvailableScoreBean =" + availableScoreBean);
                        if (mView != null) {
                            if (availableScoreBean == null) {
                                mView.showDefaultAvailableScore();
                            } else {
                                mView.showAvailableScore(availableScoreBean);
                            }

                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.showDefaultAvailableScore();
                        }
                    }
                }));
    }

    @Override
    public void createOrder(RequestBody body) {
        addSubscribe(mDataManager.creatOrder(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<OrderBean>handleResult())
                .subscribeWith(new CommonSubscriber<OrderBean>() {
                    @Override
                    public void onSuccess(OrderBean orderBean) {
                        Log.d(TAG, "orderBean =" + orderBean);
                        if (mView != null) {
                            if (orderBean != null) {
                                mView.showCreateOrderSuccess(orderBean.getOrderNo());
                            } else {
                                mView.showCreateOrderFail();
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.showCreateOrderFail();
                        }
                    }
                }));
    }

    @Override
    public void verifyInviteCode(String inviteCode) {
        addSubscribe(mDataManager.verifyInviteCode(inviteCode)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<InviteCodeBean>handleResult())
                .subscribeWith(new CommonSubscriber<InviteCodeBean>() {
                    @Override
                    public void onSuccess(InviteCodeBean inviteCodeBean) {
                        Log.d(TAG, "inviteCodeBean =" + inviteCodeBean);
                        if (mView != null) {
                            if (inviteCodeBean != null) {
                                if (inviteCodeBean.getUserName() != null) {
                                    mView.showInviteCodeValid();
                                } else {
                                    mView.showInviteCodeInvalid();
                                }

                            } else {
                                mView.showInviteCodeInvalid();
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.showInviteCodeInvalid();
                        }
                    }
                }));
    }
}
